/* Tables 1 and 3 data*/

set more off

use "Analysis Data/voteshare_data.dta", clear

rename cand_party party_name

*Merging in vote-buyer specification
merge m:1 state_name pc_num party_name using "Analysis Data/votebuying.dta", keep(matched)

*Control means for footnote of Table 1
bys state_name ac_num: egen min_voteshare_2014 = min(voteshare_2014)
bys state_name ac_num: egen avg_voteshare_2014 = mean(voteshare_2014)
bys state_name ac_num: egen max_voteshare_2014 = max(voteshare_2014)
mean min_v avg_v max_v if treat == 0 [aweight=wgt_treat]

*Number of vote-buying parties and vote-buyer vote-shares
bys state_name ac_num: egen voteshare_vb_2014 = sum(voteshare_2014)
bys state_name ac_num: egen voteshare_vb_2009 = sum(voteshare_2009)
bys state_name ac_num: egen num_vb_parties = count(cand_votes_2014)

bys state_name ac_num: drop if _n >1

drop cand_votes* voteshare_???? party_name

order state_name ac_num pc_num voteshare_vb_2014 voteshare_vb_2009

merge m:1 state_name ac_num using "Analysis Data/AC_est_sample.dta", keep(matched) nogen

tempfile temp2
save `temp2'

*Turnout rate
use "Raw Data/results_10states2009_clean.dta", clear
keep state_name ac_num total_ac_votes
rename total_ac_votes total_ac_votes_2009
bys state_name ac_num: drop if _n>1

merge 1:1 state_name ac_num using "Raw Data/electors_2009.dta", keep(matched) nogen keepusing(electors_2009)

gen turnoutrate_2009 = 100*total_ac_votes_2009/electors_2009
keep state_name ac_num total_ac_votes_2009 electors_2009 turnoutrate_2009

merge m:1 state_name ac_num using "Analysis Data/AC_est_sample.dta", keep(matched) nogen

tempfile temp3
save `temp3'

use "Raw Data/results_10states2014_clean.dta", clear
keep state_name ac_num total_ac_votes ac_electors
rename total_ac_votes total_ac_votes_2014
rename ac_electors electors_2014

bys state_name ac_num: drop if _n>1

gen turnoutrate_2014 = 100*total_ac_votes_2014/electors_2014
keep state_name ac_num total_ac_votes_2014 electors_2014

merge m:1 state_name ac_num using "Analysis Data/AC_est_sample.dta", keep(matched) nogen

merge 1:1 state_name ac_num using `temp3', keep(matched) nogen

gen turnoutrate_2014 = 100*total_ac_votes_2014/electors_2014

merge 1:1 state_name ac_num using `temp2', keep(matched master) nogen

tab poll_date, gen(poll_date)

save "Analysis Data/table1_3_data.dta", replace
outsheet using "Table 1_3\table1_3_data.csv", replace comma nolabel
outsheet using "Table C5\tableC5_data.csv", replace comma nolabel
